﻿$(document).ready(function(){

	var APP_ID="631596070191718";
	var first_open = 1;
	var fir = "1";
	//window.fbAsyncInit = initFacebook;
	$("#facebook").click(
	function initFacebook()
	{
		FB.init({
		  appId  : APP_ID,
		  status : true, // check login status
		  cookie : false, // enable cookies to allow the server to access the session
		  xfbml  : true  // parse XFBML
		});

		FB.getLoginStatus(onFacebookLoginStatus);

	});

	(function() {
		var e = document.createElement('script');
		e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
		e.async = true;
		document.getElementById('fb-root').appendChild(e);
		}());


	
	function facebookLogout()
	{
		FB.logout();
		var loginButtonDiv=document.getElementById("fb-login-button-div");
		loginButtonDiv.style.display="block";	
		var logoutButtonDiv=document.getElementById("fb-logout-button-div");
		logoutButtonDiv.style.display="none";	
		var contentDiv=document.getElementById("user-is-authenticated-div");
		contentDiv.style.display="none";			
	}

	function facebookLogin()
	{
		
		var loginUrl="http://www.facebook.com/dialog/oauth/?"+
			"scope=publish_stream&"+
			"client_id="+APP_ID+"&"+
			"redirect_uri="+document.location.href+"&"+
			"response_type=token";	
			first_open = 0;	
	
		window.location=loginUrl;
		
		postToWallUsingFBUiAndAttachment();
		
	}

	/*
	* Callback function for FB.login
	*/
	function onFacebookLoginStatus(response)
	{
		if (response.status=="connected" && response.authResponse)
		{
			var loginButtonDiv=document.getElementById("fb-login-button-div");
			loginButtonDiv.style.display="block";
			var logoutButtonDiv=document.getElementById("fb-logout-button-div");
			logoutButtonDiv.style.display="block";
			var contentDiv=document.getElementById("user-is-authenticated-div");
			contentDiv.style.display="block";	
					
			postToWallUsingFBUiAndAttachment();
		}
		else
		{
			var loginButtonDiv=document.getElementById("fb-login-button-div");
			loginButtonDiv.style.display="block";	
			var contentDiv=document.getElementById("user-is-authenticated-div");
			contentDiv.style.display="block";	
			facebookLogin();		
		}

	}  
	

	
	function postToWallUsingFBApi()
	{
		var data=
		{
			message: "Posted using FB.api",
			display: 'iframe',
			caption: "Caption",
			name: "Name",  
			picture: 'https://valspar2013.thetigerparty.com/images/brooke-burke.jpg',    
			link: "http://www.permadi.com/",  // Go here if user click the picture
			description: "Description field",
			actions: [{ name: 'action_links text!', link: 'http://www.permadi.com' }],			
		}
		//console.log(data);    
		FB.api('/me/feed', 'post', data, onPostToWallCompleted);
	}

	function postToWallUsingFBUiAndAttachment()
	{	
	
		var data=
		{
			method: 'stream.publish',  
			message: "Posted using FB.ui.",
			display: 'iframe',
			attachment: 
			{
				media: [{  
						type: "image", 
						src: 'https://valspar2013.thetigerparty.com/images/brooke-burke.jpg',    
						href: "https://valspar2013.thetigerparty.com/"  // Go here if user click the picture
				}],              	
				name: "Attachment Name",
				caption: 'Attachment Caption',
				href: "http://www.permadi.com/"			// Go here is user clicks the Attachment Name
			},			
			href: "http://www.permadi.com/",
			action_links: [
                { text: 'action_links text!', href: 'http://www.permadi.com' }
            ],			
		}
		FB.ui(data, onPostToWallCompleted);
		
	}
	
	function postToWallUsingFBUi()
	{	
		var data=
		{
			method: 'stream.publish',  
			message: "Posted using FB.ui and picture.",
			display: 'iframe',
			caption: "Caption",
			name: "Name",
			picture: 'https://valspar2013.thetigerparty.com/images/brooke-burke.jpg',    
			link: "http://www.permadi.com/",  // Go here if user click the picture
			description: "Description field",
			actions: [{ name: 'action_links text!', link: 'https://valspar2013.thetigerparty.com/' }],			
		}
		FB.ui(data, onPostToWallCompleted);
	}
	
	function onPostToWallCompleted(response)
	{
		if (response)
		{
			//console.log(response);
			if (response.error)
			{
				alert(response.error.message);
			}
			else
			{
				if (response.id)
					alert("Posted as post_id "+response.id);						
				else if (response.post_id)
					alert("Posted as post_id "+response.post_id);
				else
					alert("Unknown Error");
			}
		}
		// user cancelled
	}
	
});

  
function SetLtbCssSelected(link_name)
{//設定lightbox選中大小圖的css、圖檔來源
	//$("div[id^=link]").attr("src","images/StacyKeibler.jpg");  //將大圖替換成目前選中的圖
	$("#img-lightbox").attr("src","images/"+link_name+".jpg");
	$("#title-lightbox").attr("src","images/title-"+link_name+".png");
	changeImgSize(link_name);
}

function changeImgSize(link_name)
{
	var newImg = new Image();
	newImg.src = "images/"+link_name+".png";
	$("#img-lightbox").attr("height","newImg.height");
	$("#img-lightbox").attr("width","newImg.width");
} 